package com.css.test1;

import android.util.Log;

import org.jsoup.nodes.Document;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by css on 2017/11/6.
 */

public class Net {
    private OkHttpClient client = new OkHttpClient();
    private boolean isSuccess = false;

    //http://ecard.avceit.cn:227/index.action
    private Document doc;//从字符串解析html


    /**
     * 银行圈存
     *
     * @param string
     */
    public void send(String string) {
        FormBody body = new FormBody.Builder()
                .add("mobile", string)
                .build();
        final Request request = new Request.Builder()
                .url("https://epay.10010.com/partyServer/dzq/sendMsg.do")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                netConnectListener.gettransferMoney(false, "");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("----", response.body().string());
                if (response.isSuccessful()) {
                    String content = response.body().string();
                    Log.d("ssss", content);
//                    content = content.substring(content.indexOf("<body"), content.indexOf("</body>") + 7);
                    String token = content.substring(content.indexOf("\"usr_token\":\"") + 13, content.indexOf("\"}"));
                    listener.send(token);
                }
            }
        });
    }


    /**
     * 银行圈存
     */
    public void code(String token, String code) {

/*mobile:15656858300
validateCode:123
usr_token:201711061653786490
channelType:ZLSCQMMQD*/
        FormBody body = new FormBody.Builder()
                .add("mobile", "13083012203")
                .add("validateCode", code)
                .add("usr_token", token)
                .add("channelType", "ZLSCQMMQD")
                .build();
        final Request request = new Request.Builder()
                .url("https://epay.10010.com/partyServer/dzq/testVercode.do")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                netConnectListener.gettransferMoney(false, "");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("----", response.body().string());
                if (response.isSuccessful()) {
                    String content = response.body().string();
                    Log.d("ssss", content);
//                    content = content.substring(content.indexOf("<body"), content.indexOf("</body>") + 7);
                }
            }
        });
    }

    /**
     * 银行圈存
     */
    public void getCode(String tel) {

/*mobile:15656858300
validateCode:123
usr_token:201711061653786490
channelType:ZLSCQMMQD*/
        FormBody body = new FormBody.Builder()
                .add("action", "getMessage")
                .add("phone", tel)
                .add("sid", "53325")
                .add("token", "814dea35-5f5e-4ff8-827a-204dc64cce31")
                .add("author", "")
                //[Full request URI: http://api.xingjk.cn/api/do.php]
                .build();
        final Request request = new Request.Builder()
                .url("http://api.xingjk.cn/api/do.php")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                netConnectListener.gettransferMoney(false, "");
                int i = 0;
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("----", response.body().string());
                if (response.isSuccessful()) {
                    String content = response.body().string();
                    Log.d("tel", content);
//                    content = content.substring(content.indexOf("<body"), content.indexOf("</body>") + 7);
                }
            }
        });
    }
    /**
     * 银行圈存
     */
    public void getTel() {

/*mobile:15656858300
validateCode:123
usr_token:201711061653786490
channelType:ZLSCQMMQD*/
        FormBody body = new FormBody.Builder()
                .add("action", "getPhone")
                .add("sid", "53325")
                .add("token", "814dea35-5f5e-4ff8-827a-204dc64cce31")
                .add("showExtra", "1")
                .add("phoneType", "")
                //[Full request URI: http://api.xingjk.cn/api/do.php]
                .build();
        final Request request = new Request.Builder()
                .url("http://api.xingjk.cn/api/do.php")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                netConnectListener.gettransferMoney(false, "");
                int i = 0;
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.e("----", response.body().string());
                if (response.isSuccessful()) {
                    String content = response.body().string();
                    Log.d("tel", content);
//                    content = content.substring(content.indexOf("<body"), content.indexOf("</body>") + 7);
                }
            }
        });
    }

    private Listener listener;

    public void addListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void send(String token);
    }
}
